﻿using UnityEngine;
using System.Collections;

namespace LuaFramework {
    public class GameManager : Manager
    {
        Ctrl mCtrl;

        /// <summary>
        /// 初始化游戏管理器
        /// </summary>
        void Awake()
        {
            Screen.sleepTimeout = SleepTimeout.NeverSleep;

            mCtrl = new Ctrl();
        }

        public void ChangeState(string stateName, string[] loadSceneNames = null, params object[] additionalParams)
        {
            mCtrl.changeProc(stateName, loadSceneNames, additionalParams);
        }

        public Proc CurrentState()
        {
            return mCtrl.CurrProc;
        }

        public string CurrentStateName()
        {
            return mCtrl.CurrProc.type;
        }

        new void Update()
        {
            FPSCalculator.Instance.Tick(Time.unscaledDeltaTime);

            if (mCtrl != null)
            {
                mCtrl.Update();
            }
        }

        private void FixedUpdate()
        {
            if (mCtrl != null)
                mCtrl.FixedUpdate();

            //behaviac.Workspace.Instance.DebugUpdate();
        }

        void OnApplicationFocus(bool focus)
        {
            if (AppFacade.luaManager.lua!=null)
                AppFacade.luaManager.lua.Call<bool>("CSCall_OnApplicationFocus", focus, false);
        }

        void OnApplicationPause(bool pause)
        {

            if (AppFacade.luaManager.lua != null)
                AppFacade.luaManager.lua.Call<bool>("CSCall_OnApplicationPause", pause, false);
        }

        void OnApplicationQuit()
        {
            AppFacade.isShuttingDown = true;

            if (AppFacade.luaManager.lua != null)
                AppFacade.luaManager.lua.Call("CSCall_OnApplicationQuit", false);
        }
        

        /// <summary>
        /// 析构函数
        /// </summary>
        void OnDestroy() {
            Debug.Log("~GameManager was destroyed");
        }
    }
}